package cn.iocoder.yudao.module.biz.service.risk.hazardResource;

import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.biz.controller.admin.risk.hazardResource.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.hazardResource.BizHazardResourceConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.hazardResource.BizHazardResourceDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.hazardResource.BizHazardResourceMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 危险源 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizHazardResourceServiceImpl implements BizHazardResourceService {

    @Resource
    private BizHazardResourceMapper hazardResourceMapper;

    @Override
    public String createHazardResource(BizHazardResourceCreateReqVO createReqVO) {
        // 插入
        BizHazardResourceDO hazardResource = BizHazardResourceConvert.INSTANCE.convert(createReqVO);
        hazardResourceMapper.insert(hazardResource);
        // 返回
        return hazardResource.getId();
    }

    @Override
    public void updateHazardResource(BizHazardResourceUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHazardResourceExists(updateReqVO.getId());
        // 更新
        BizHazardResourceDO updateObj = BizHazardResourceConvert.INSTANCE.convert(updateReqVO);
        hazardResourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteHazardResource(String id) {
        // 校验存在
        this.validateHazardResourceExists(id);
        // 删除
        hazardResourceMapper.deleteById(id);
    }

    private void validateHazardResourceExists(String id) {
        if (hazardResourceMapper.selectById(id) == null) {
            throw exception(HAZARD_RESOURCE_NOT_EXISTS);
        }
    }

    @Override
    public BizHazardResourceDO getHazardResource(String id) {
        return hazardResourceMapper.selectById(id);
    }

    @Override
    public List<BizHazardResourceDO> getHazardResourceList(Collection<String> ids) {
        return hazardResourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizHazardResourceDO> getHazardResourcePage(BizHazardResourcePageReqVO pageReqVO) {
        return hazardResourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizHazardResourceDO> getHazardResourceList(BizHazardResourceExportReqVO exportReqVO) {
        return hazardResourceMapper.selectList(exportReqVO);
    }

    @Override
    public void deleteHazardResourceByRiskId(String riskId) {
        hazardResourceMapper.delete(new QueryWrapperX<BizHazardResourceDO>().eqIfPresent("risk_id",riskId));
    }

    @Override
    public List<BizHazardResourceRespVO> getHazardResourceByRiskId(String riskId) {
        return hazardResourceMapper.getHazardResourceByRiskId(riskId);
    }

}
